//! # Binance SDK Unified
//! 
//! 统一的Binance SDK，支持所有产品线：
//! - Spot (现货)
//! - USDM (USDT合约)
//! - COINM (币本位合约)  
//! - Options (期权)
//! - Portfolio Margin (组合保证金)

pub mod config;
pub mod client;
pub mod rest;
pub mod websocket;
pub mod models;
pub mod errors;

// 重新导出主要类型
pub use client::BinanceClient;
pub use config::BinanceConfig;
pub use errors::{BinanceError, Result};

/// 产品类型枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ProductType {
    /// 现货交易
    #[serde(rename = "Spot")]
    Spot,
    /// USDT合约
    #[serde(rename = "UsdM")]
    UsdM,
    /// 币本位合约
    #[serde(rename = "CoinM")]
    CoinM,
    /// 期权
    #[serde(rename = "Options")]
    Options,
    /// 组合保证金
    #[serde(rename = "Portfolio")]
    Portfolio,
}

impl ProductType {
    /// 获取REST API基础URL
    pub fn rest_base_url(&self, env: Environment) -> &'static str {
        match (self, env) {
            // 现货交易
            (ProductType::Spot, Environment::Live) => "https://api.binance.com",
            (ProductType::Spot, Environment::Demo) => "https://demo-api.binance.com",
            
            // USDT永续合约
            (ProductType::UsdM, Environment::Live) => "https://fapi.binance.com",
            (ProductType::UsdM, Environment::Demo) => "https://demo-fapi.binance.com",
            
            // 币本位合约
            (ProductType::CoinM, Environment::Live) => "https://dapi.binance.com",
            (ProductType::CoinM, Environment::Demo) => "https://demo-dapi.binance.com",
            
            // 期权
            (ProductType::Options, Environment::Live) => "https://eapi.binance.com",
            (ProductType::Options, Environment::Demo) => "https://demo-api.binance.com", // Demo模式使用现货Demo端点
            
            // 组合保证金
            (ProductType::Portfolio, Environment::Live) => "https://api.binance.com",
            (ProductType::Portfolio, Environment::Demo) => "https://demo-api.binance.com",
        }
    }

    /// 获取WebSocket基础URL
    pub fn ws_base_url(&self, env: Environment) -> &'static str {
        match (self, env) {
            // 现货交易
            (ProductType::Spot, Environment::Live) => "wss://stream.binance.com:9443/ws",
            (ProductType::Spot, Environment::Demo) => "wss://demo-ws-api.binance.com/ws",
            
            // USDT永续合约
            (ProductType::UsdM, Environment::Live) => "wss://fstream.binance.com/ws",
            (ProductType::UsdM, Environment::Demo) => "wss://fstream.binancefuture.com/ws",
            
            // 币本位合约
            (ProductType::CoinM, Environment::Live) => "wss://dstream.binance.com/ws",
            (ProductType::CoinM, Environment::Demo) => "wss://dstream.binancefuture.com/ws",
            
            // 期权
            (ProductType::Options, Environment::Live) => "wss://eapi.binance.com/ws",
            (ProductType::Options, Environment::Demo) => "wss://demo-ws-api.binance.com/ws", // Demo模式使用现货Demo WebSocket
            
            // 组合保证金
            (ProductType::Portfolio, Environment::Live) => "wss://stream.binance.com:9443/ws",
            (ProductType::Portfolio, Environment::Demo) => "wss://demo-ws-api.binance.com/ws",
        }
    }

    /// 获取基础API路径（不包含版本号）
    pub fn base_api_prefix(&self) -> &'static str {
        match self {
            ProductType::Spot => "/api",
            ProductType::UsdM => "/fapi",
            ProductType::CoinM => "/dapi",
            ProductType::Options => "/eapi",
            ProductType::Portfolio => "/sapi",
        }
    }
}

/// 环境类型
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Environment {
    /// 生产环境
    #[serde(rename = "Live")]
    Live,
    /// Demo模式 - 模拟交易环境
    #[serde(rename = "Demo")]
    Demo,
}

impl Default for Environment {
    fn default() -> Self {
        Environment::Demo
    }
}
